#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

# 阶段1：版本生成阶段（使用本地版本文件）
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS version-generator
WORKDIR /src

COPY version.txt .

# 阶段2：基础镜像
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080

# 阶段3：构建阶段
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release

# 从版本生成阶段复制版本文件 - 使用数字索引而非名称
COPY --from=version-generator /src/version.txt .

RUN cat version.txt && echo "Building with version: $(cat version.txt)"

WORKDIR /src
COPY ["EOM.TSHotelManagement.WebApi/EOM.TSHotelManagement.WebApi.csproj", "EOM.TSHotelManagement.WebApi/"]
COPY ["EOM.TSHotelManagement.Shared/EOM.TSHotelManagement.Shared.csproj", "EOM.TSHotelManagement.Shared/"]
COPY ["EOM.TSHotelManagement.Application/EOM.TSHotelManagement.Application.csproj", "EOM.TSHotelManagement.Application/"]
COPY ["EOM.TSHotelManagement.Common.Contract/EOM.TSHotelManagement.Common.Contract.csproj", "EOM.TSHotelManagement.Common.Contract/"]
COPY ["EOM.TSHotelManagement.Migration/EOM.TSHotelManagement.Migration.csproj", "EOM.TSHotelManagement.Migration/"]
COPY ["EOM.TSHotelManagement.Common.Core/EOM.TSHotelManagement.Common.Core.csproj", "EOM.TSHotelManagement.Common.Core/"]
COPY ["EOM.TSHotelManagement.Common.Util/EOM.TSHotelManagement.Common.Util.csproj", "EOM.TSHotelManagement.Common.Util/"]
COPY ["EOM.TSHotelManagement.EntityFramework/EOM.TSHotelManagement.EntityFramework.csproj", "EOM.TSHotelManagement.EntityFramework/"]
RUN dotnet restore "EOM.TSHotelManagement.WebApi/EOM.TSHotelManagement.WebApi.csproj"
COPY . .
WORKDIR "/src/EOM.TSHotelManagement.WebApi"

# 使用版本号构建
RUN VERSION=$(cat /src/version.txt | tr -d '\r' | tr -cd '[:digit:].') && \
    echo "Using cleaned version: $VERSION" && \
    dotnet build "EOM.TSHotelManagement.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/build /p:Version=$VERSION

# 阶段4：发布阶段
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN VERSION=$(cat /src/version.txt | tr -d '\r' | tr -cd '[:digit:].') && \
    dotnet publish "EOM.TSHotelManagement.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:Version=$VERSION /p:UseAppHost=false

# 阶段5：最终镜像
FROM base AS final
WORKDIR /app

RUN mkdir -p /app/keys && \
    chown app:app /app/keys && \
    chmod 700 /app/keys

ENV ASPNETCORE_DATAPROTECTION_DIRECTORY="/app/keys"

COPY --from=version-generator /src/version.txt .
COPY --from=publish /app/publish .

ENTRYPOINT ["dotnet", "EOM.TSHotelManagement.WebApi.dll"]